package net.com.bwie;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;

import java.util.HashMap;
import java.util.Map;

public class ParselpToRegion extends UDF {
    public Map<String,String> evaluate(String strip) throws Exception{
        HashMap<String, String> map = new HashMap<>();
        DbSearcher dbSearcher = new DbSearcher(
                new DbConfig(),
//                "D:\\2211A\\workspace\\data_warehouse\\jtp_ads_warehouse\\src\\main\\resources\\ip2region.db"
                "/home/bwie/ip2region.db"
        );
        String region = dbSearcher.binarySearch(strip).getRegion();
        String[] str = region.split("\\|");
        map.put("country",str[0]);
        map.put("area",str[1]);
        map.put("province",str[2]);
        map.put("city",str[3]);
        map.put("isp",str[4]);
        return map;
    }

    public static void main(String[] args) throws Exception{
        System.out.println(new ParselpToRegion().evaluate("142.70.109.225"));
    }
}
